package com.qlm.hbutil;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qlm.log.Log;

public class HbResult {

	private static Logger logger = LoggerFactory.getLogger(HbResult.class);

	private boolean status = false;
	
	private Date payTime = null;//支付成功后的实际支付时间
	
	private String payNOByWX = "";//支付成功后微信商户平台的单号
	
	private String payNo = "";//支付的单号
	
	private String error_code = "";//报错编码
	
	private String error_msg = "";//发送失败后返回信息
	
	private String return_msg = "";//发红包后返回到前台的信息
	
	private int return_status = -1;//发红包返回的标志位
	
	public static HbResult sendHb(String openid,int money,String orderno,String act_name){
		HbResult result = new HbResult();
		result.payNo = orderno;
		URL resource = HbResult.class.getResource("/apiclient_cert.p12");
		String file = resource.getFile();
		Map<String, String> resultMap = null ;
		try{
			resultMap = HbUtil.transfersRequest(openid, money, file, orderno,act_name);
//			resultMap = new HashMap<String, String>();
//			resultMap.put("err_code", "V2_ACCOUNT_SIMPLE_BAN");
////			resultMap.put("err_code_des", "ssd");
			String state = resultMap.get("result_code");
			HbEnum resultEnum = null;
			if ("SUCCESS".equals(state)) {
				String payment_no = resultMap.get("payment_no");
				String time = resultMap.get("payment_time");
				result.status = true;
				result.payNOByWX = payment_no;
				result.payTime = formatTime(time);
				resultEnum = HbEnum.Success;
			}else{
				String err_code = resultMap.get("err_code");
				String errer_code_desc = resultMap.get("err_code_des");
				result.error_code = err_code;
				result.error_msg = errer_code_desc;
				if("NO_AUTH".equals(err_code)){//提示当扫码用户的账号被微信冻结时，无法完成付款
					resultEnum = HbEnum.UnAuth;
				}else if("非实名用户账号不可发放".equals(err_code) || "V2_ACCOUNT_SIMPLE_BAN".equals(err_code)){
					resultEnum = HbEnum.UnName;
				}else {
					resultEnum = HbEnum.NetSlow;
				}
			}
			result.return_msg = resultEnum.getMsg();
			result.return_status = resultEnum.getNum();
		}catch(Exception e){
			e.printStackTrace();
			Log.info("发送红包网络出错,openid="+openid, e);
			result.return_msg = HbEnum.NetSlow.getMsg();
			result.return_status = HbEnum.NetSlow.getNum();
		}
		
		return result;
	}
	
	public String getError_code() {
		return error_code;
	}

	public String getError_msg() {
		return error_msg;
	}

	public String getReturn_msg() {
		return return_msg;
	}
	public int getReturn_status() {
		return return_status;
	}

	private static Date formatTime(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date parse = sdf.parse(time);
			return parse;
		} catch (Exception e) {
			return new Date();
		}
	}
	
	public boolean isSuccess(){
		return status;
	}
	
	
	
	public Date getPayTime() {
		return payTime;
	}



	public String getPayNOByWX() {
		return payNOByWX;
	}



	public String getPayNo() {
		return payNo;
	}



	
}
